package com.xzq.daoimpl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.xzq.dao.AqiDao;
import com.xzq.entity.Aqi;
import com.xzq.entity.District;
import com.xzq.entity.Page;
import com.xzq.util.HibernatePage;
@Repository("aqiDao")
public class AqiDaoImpl extends HibernatePage<Aqi> implements AqiDao {

	@Override
	public Page findAllAqiByPage(Page page,Integer type) {
		if(type==0){
			String hql = "from Aqi a where a.dis is not null order by a.id";
			page = findHibernatePage(page, hql, null, page.AIRSIZE);
		}else{
			String hql = "from Aqi a where a.dis is not null and a.dis.id = ? order by a.id";
			Object[] obj = {type};
			page = findHibernatePage(page, hql, obj, page.AIRSIZE);
		}
		return page;
	}

	@Override
	public Aqi findOneAqi(Aqi aqi) {
		aqi = template.get(Aqi.class, aqi.getId());
		return aqi;
	}

	@Override
	public Integer saveAqi(Aqi aqi) {
		Integer count = -1;
		count = (Integer) template.save(aqi);
		return count;
	}

	@Override
	public Integer modifyAqi(Aqi aqi) {
		try {
			template.update(aqi);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public Integer deleteAqi(Aqi aqi) {
		try {
			template.delete(aqi);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public List<District> findAllDis() {
		String hql = "from District";
		List<District> list = template.find(hql);
		return list;
	}

	@Override
	public District findOneDis(District dis) {
		dis = template.get(District.class, dis.getId());
		return dis;
	}

	@Override
	public Integer saveDis(District dis) {
		Integer count = -1;
		count = (Integer) template.save(dis);
		return count;
	}

	@Override
	public Integer modifyDis(District dis) {
		try {
			template.update(dis);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public Integer deleteDis(District dis) {
		try {
			template.delete(dis);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public Page findAllDisByPage(Page page, String likename) {
		String hql = "from District where name like ?";
		Object[] obj = {"%"+likename+"%"};
		page = findHibernatePage(page, hql, obj, page.DISSIZE);
		return page;
	}

}
